//
//  XDMailDetailViewController.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-6-3.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "XDMailDetailViewController.h"
#import "StringUtil.h"
#import "Mail_GetDetail_Task.h"
#import "TSActionSheet.h"
#import "XDFileDownloadViewController.h"
#import "FileDownloadModel.h"
#import "ATTACHList.h"
#import "DBFrameService.h"
#import "XDAttachCell.h"
#import "AttachDownloadTask.h"
#import "CommonUtil.h"
#import "XDMailSendViewController.h"

@interface XDMailDetailViewController ()
{
    NSString *boxType;
    NSString *mailguid;
    NSString *getMailDetailTaskGuid,*downloadtaskguid;
    NSMutableArray *attachList;
    int selectedIndex;
    NSString *Subject,*MailContent,*FROMDISPNAME,*SENDTIME,*ToUserDispNameList,*ToUserGuidList,*FromUserGuid;
}
@end

@implementation XDMailDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    boxType = [intentParams objectForKey:@"boxType"];
    mailguid = [[intentParams objectForKey:@"mailguid"]retain];
    self.title = @"邮件详情";
    [self.tabBar setSelectedItem:self.tbNr];
    [self.tabBar setDelegate:self];
    NSMutableDictionary *params = self.getPassDictionary;
    [params setObject:mailguid forKey:@"MailGuid"];
    [self showMBPHUD];
    getMailDetailTaskGuid = [[[Mail_GetDetail_Task alloc]initWithContext:self andParams:params]startTask];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"操作" style:UIBarButtonItemStyleBordered target:self action:@selector(showActionSheet:forEvent:)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_wv release];
    [_tabBar release];
    [_tbFj release];
    [_tbNr release];
    [_tvFj release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setWv:nil];
    [self setTabBar:nil];
    [self setTbFj:nil];
    [self setTbNr:nil];
    [self setTvFj:nil];
    [super viewDidUnload];
}

#pragma controller

-(void)refreshUIWithThreadGuid:(NSString *)threadGuid andData:(NSObject *)obj
{
    [self hideMBPHUD];
    if ([getMailDetailTaskGuid isEqualToString:threadGuid]) {
        NSString *bs = (NSString *)obj;
        
        if ([self validateXML:bs]) {
            
            NSLog(@"%@",bs);
            
            Subject = [[StringUtil getXMLFormatedAttribute:bs :@"Subject"]retain];
            FROMDISPNAME = [[StringUtil getXMLFormatedAttribute:bs :@"FROMDISPNAME"]retain];
            SENDTIME = [[StringUtil getXMLFormatedAttribute:bs :@"SENDTIME"]retain];
            MailContent = [[StringUtil switchXML2Origion:[StringUtil getXMLFormatedAttribute:bs :@"MailContent"]]retain];
            FromUserGuid = [[StringUtil getXMLFormatedAttribute:bs :@"FromUserGuid"]retain];
            
            ToUserDispNameList = [[StringUtil getXMLFormatedAttribute:bs :@"ToUserDispNameList"]retain];
            ToUserGuidList = [[StringUtil getXMLFormatedAttribute:bs :@"ToUserGuidList"]retain];
            NSString *ChaoSongUserDispNameList = [[StringUtil getXMLFormatedAttribute:bs :@"ChaoSongUserDispNameList"]retain];
            
            NSString *csrDisp = @"";
            if (![ChaoSongUserDispNameList isEqualToString:@""]) {
                csrDisp = [NSString stringWithFormat:@"<hr/><div id='oschina_outline'>抄送人：%@</div>",ChaoSongUserDispNameList];
            }
            
            NSString *disContent = [NSString stringWithFormat:@"%@<div id='oschina_title'>%@</div><div id='oschina_outline'>%@ %@</div><hr/><div id='oschina_outline'>收件人：%@</div>%@<hr />%@",HTMLSTYPE1,Subject,FROMDISPNAME,SENDTIME,ToUserDispNameList,csrDisp,MailContent];
            
            [self.wv loadHTMLString:disContent baseURL:nil];
            
            attachList = [[Mail_GetDetail_Task getAttackList:bs]retain];
            
            if (attachList.count>0) {
                self.tbFj.badgeValue = [NSString stringWithFormat:@"%i",attachList.count];
            }
            
        }
    }
    else if ([downloadtaskguid isEqualToString:threadGuid])
    {
        NSString *path = (NSString *)obj;
        [self openFileFromSecondApp:path];
    }
}

-(void)showActionSheet:(id)sender forEvent:(UIEvent*)event
{
    TSActionSheet *actionSheet = [[TSActionSheet alloc] initWithTitle:@"请选择"];
    [actionSheet addButtonWithTitle:[NSString stringWithFormat:@"转发"] block:^{
        
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:@"3" forKey:@"EnterFlag"];
        [params setObject:Subject forKey:@"MailTitle"];
        [params setObject:FROMDISPNAME forKey:@"FROMDISPNAME"];
        [params setObject:MailContent forKey:@"MailContent"];
        [params setObject:SENDTIME forKey:@"SENDTIME"];
        [params setObject:attachList forKey:@"zfAttachList"];
        
        
        XDMailSendViewController *vc = [[XDMailSendViewController alloc]initWithParams:params];
        [self.navigationController pushViewController:vc animated:YES];
        
    }];
    if ([boxType isEqualToString:@"100000"]) {
        
        
        [actionSheet addButtonWithTitle:[NSString stringWithFormat:@"回复"] block:^{
            NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
            [params setObject:@"2" forKey:@"EnterFlag"];
            [params setObject:Subject forKey:@"MailTitle"];
            [params setObject:FROMDISPNAME forKey:@"FROMDISPNAME"];
            [params setObject:MailContent forKey:@"MailContent"];
            [params setObject:SENDTIME forKey:@"SENDTIME"];
            [params setObject:[NSString stringWithFormat:@"%@;",FROMDISPNAME] forKey:@"ToUserDispNameList"];
            [params setObject:[NSString stringWithFormat:@"%@;",FromUserGuid] forKey:@"ToUserGuidList"];
            [params setObject:[NSString stringWithFormat:@"%@;",FromUserGuid] forKey:@"guids"];
            [params setObject:[NSString stringWithFormat:@"%@;",FROMDISPNAME] forKey:@"names"];
            
            
            
            XDMailSendViewController *vc = [[XDMailSendViewController alloc]initWithParams:params];
            [self.navigationController pushViewController:vc animated:YES];

        }];
        
    }

        actionSheet.cornerRadius = 2;
        [actionSheet showWithTouch:event];
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item.tag==2001) {
        self.wv.hidden = NO;
        self.tvFj.hidden = YES;
        selectedIndex = 0;
    }
    else if (item.tag==2002) {
        self.wv.hidden = YES;
        self.tvFj.hidden = NO;
        selectedIndex = 1;
        [self.tvFj reloadData];
    }
}

#pragma tableview

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (selectedIndex==1) {
        static NSString *identifier = @"XDFlowInfoCell";
        XDAttachCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"XDAttachCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        
        ATTACHList *m = [attachList objectAtIndex:indexPath.row];
        cell.lbAttachName.text = m.ATTACHFILENAME;
        return cell;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (selectedIndex==1) {
        return 44;
    }
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (selectedIndex==1) {
        ATTACHList *m = [attachList objectAtIndex:indexPath.row];
        if ([m.isBigFile isEqualToString:@"1"]) {
            [UITool toast:@"大附件无法下载！"];
            return;
        }
        
        NSString *savePath = [NSString stringWithFormat:@"%@/Documents/attaches/%@/%@",NSHomeDirectory(),mailguid,m.ATTACHFILENAME];
        NSString *downloadurl = [[CommonUtil getDownloadUrl:m.DownLoadUrl]retain];
        NSMutableDictionary *params = self.getPassDictionary;
        [params setObject:savePath forKey:@"savePath"];
        [params setObject:downloadurl forKey:@"url"];
        [params setObject:mailguid forKey:@"folerguid"];
        [self showMBPHUD];
        downloadtaskguid = [[[AttachDownloadTask alloc]initWithContext:self andParams:params]startTask];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (selectedIndex==1) {
        return attachList.count;
    }
    return 0;
}

@end
